package cn.uikit.demo.wheel

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.graphics.toColorInt
import cn.uikit.demo.databinding.ActivityWheelBinding
import cn.uikit.demo.databinding.ItemWheelVerticalBinding
import sivin.uikit.adapter2.SimpleAdapter
import sivin.uikit.framework.BindingActivity
import sivin.uikit.utils.dp

class WheelActivity : BindingActivity<ActivityWheelBinding>(ActivityWheelBinding::inflate) {

    companion object {
        fun launch(context: Context) {
            val startIntent = Intent(context, WheelActivity::class.java)
            context.startActivity(startIntent)
        }
    }


    data class WheelItem(val value: String, var select: Boolean = false)

    private val dataList = listOf(
        WheelItem("测试Title2", false),
        WheelItem("测试Title1sdfsdfsdfsdf", true),
        WheelItem("测试Title3", false),
        WheelItem("测试Title4", false),
        WheelItem("测试Title5", false),
        WheelItem("测试Title6", false),
        WheelItem("测试Title7", false),
    )

    private var lastSelectPos = 0

    private val adapterVertical = SimpleAdapter<WheelItem, ItemWheelVerticalBinding>(
        ItemWheelVerticalBinding::inflate
    ) { holder, item, pos ->
        holder.binding.apply {
            text.text = item.value
            text.setLines(item.value.length)
        }
    }


    override fun onViewCreate(savedInstanceState: Bundle?) {
        super.onViewCreate(savedInstanceState)
        initVertical()
    }

    private fun initVertical() {
        adapterVertical.setDataList(dataList)
        binding.wheelVertical.adapter = adapterVertical
        val wheelModule = binding.wheelVertical.setupWheelModule()
        wheelModule.apply {
            offset = 2
            orientation = RecyclerWheelViewModule.VERTICAL
            setWheelDecoration(
                DefaultWheelDecoration(
                    0f.dp,
                    0f.dp,
                    2f.dp,
                    "#dddddd".toColorInt()
                )
            )
            onScrollingSelectListener = { selectedPosition ->
                Log.d("Sivin", "initVertical: $selectedPosition")
                dataList[lastSelectPos].select = false
                dataList[selectedPosition].select = true
                lastSelectPos = selectedPosition
            }
            onRecyclerViewSizeChanged = { size ->
                binding.topShadow.layoutParams = binding.topShadow.layoutParams.apply {
                    height = size
                }
                binding.bottomShadow.layoutParams = binding.bottomShadow.layoutParams.apply {
                    height = size
                }
            }
        }
    }
}